package com.cy.train.business.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cy.train.business.req.DailyTrainQueryReq;
import com.cy.train.business.req.DailyTrainSaveReq;
import com.cy.train.business.resp.DailyTrainQueryResp;
import com.cy.train.business.service.DailyTrainService;
import com.cy.train.common.resp.CommonResp;
import com.cy.train.common.resp.PageResp;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/daily-train")
public class DailyTrainAdminController {
    @Autowired
    private DailyTrainService dailyTrainService;

    @PostMapping("/save")
    public CommonResp<Object> register(@Valid @RequestBody DailyTrainSaveReq req) {
        dailyTrainService.save(req);
        return new CommonResp<>();
    }

    @GetMapping("/query-list")
    public CommonResp<PageResp> register(@Valid DailyTrainQueryReq req) {
        if(ObjectUtil.isNull(req)) {
            req = new DailyTrainQueryReq();
        }
        PageResp<DailyTrainQueryResp> resp = dailyTrainService.queryList(req);
        return new CommonResp<>(resp);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp<Object> delete(@PathVariable Long id) {
        dailyTrainService.delete(id);
        return new CommonResp<>();
    }

    @GetMapping("/gen-train/{date}")
    public CommonResp<Object> genTrain(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        dailyTrainService.genDailyTrain(date);
        return new CommonResp<>();
    }
}